﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.DataAccess;
using System.Data.SqlClient;
using System.Data;

namespace TimKen.Web.Admin.Customer
{
    public partial class DealerManageDetail : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindResources();
                BindProvince();

                string id = Request.QueryString["DealerID"];

                if (!string.IsNullOrEmpty(id))
                {
                    string sql = "select *  FROM   Dealer  WHERE DealerID={0}";
                    sql = string.Format(sql, id);
                    SqlDataReader reader = DbHelperSQL.ExecuteReader(sql);
                    if (reader.Read())
                    {
                        this.lablDealerID.Text = reader["DealerID"].ToString();
                        this.txtDealerNameCn.Text = reader["DealerNameCn"].ToString();
                        this.txtDealerNameEn.Text = reader["DealerNameEn"].ToString();
                        this.txtUrl.Text = reader["Url"].ToString();
                        this.txtComanyName.Text = reader["CompanyName"].ToString();
                        this.txtDealerName.Text = reader["DealerName"].ToString(); ;
                        this.txtPhoneNo.Text = reader["PhoneNo"].ToString();
                        this.txtLicensedNo.Text = reader["LicensedNo"].ToString();
                        this.txtDealerName_En.Text = reader["DealerName_En"].ToString();

                        if (reader["DealerLive"] != null)
                        {
                            string DealerLive = reader["DealerLive"].ToString();
                            if (this.ddltxtDealerLive.SelectedItem != null)
                                this.ddltxtDealerLive.SelectedItem.Selected = false;

                            foreach (ListItem item in this.ddltxtDealerLive.Items)
                            {
                                if (item.Text == DealerLive)
                                    item.Selected = true;
                            }
                        }
                        if (reader["ProvinceID"] != null)
                        {
                            string provinceId = reader["ProvinceID"].ToString();
                            if (this.ddlProvince.SelectedItem != null)
                                this.ddlProvince.SelectedItem.Selected = false;

                            foreach (ListItem item in this.ddlProvince.Items)
                            {
                                if (item.Value == provinceId)
                                    item.Selected = true;
                            }
                        }
                    }
                    reader.Close();
                }
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            string DealerNameCn = this.txtDealerNameCn.Text;
            string DealerNameEn = this.txtDealerNameEn.Text;
            string url = this.txtUrl.Text;
            string DealerId = this.lablDealerID.Text;
            string LicensedNo = this.txtLicensedNo.Text;
            string DealerName_En = this.txtDealerName_En.Text;
            string provinceID = this.ddlProvince.SelectedItem.Value;
            string DealerName = this.txtDealerName.Text;
            string CompanyName = this.txtComanyName.Text;
            string DealerLive = this.ddltxtDealerLive.SelectedItem.Text;
            string PhoneNo = this.txtPhoneNo.Text;


            if (string.IsNullOrEmpty(DealerId))
            {
                string insertsql = "INSERT INTO Dealer (DealerNameCn,DealerNameEn,ProvinceID,Url,DealerName,CompanyName,DealerLive,PhoneNo,LicensedNo,DealerName_En)  VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')";
                insertsql = string.Format(insertsql, DealerNameCn, DealerNameEn, provinceID, url, DealerName, CompanyName, DealerLive, PhoneNo, LicensedNo, DealerName_En);
                DbHelperSQL.ExecuteSql(insertsql);
            }
            else
            {
                string updatesql = "UPDATE Dealer SET   DealerNameCn='{0}',DealerNameEn='{1}',ProvinceID='{2}' ,Url='{3}',DealerName='{4}',CompanyName='{5}',DealerLive='{6}',PhoneNo='{7}',LicensedNo='{8}',DealerName_En='{9}' WHERE DealerID={10}";
                updatesql = string.Format(updatesql, DealerNameCn, DealerNameEn, provinceID, url, DealerName, CompanyName, DealerLive, PhoneNo, LicensedNo, DealerName_En, DealerId);
                DbHelperSQL.ExecuteSql(updatesql);
            }
            Response.Write("<script>window.opener.refresh();window.opener=null;window.close();</script>");
        }
        protected void BindResources()
        {
            btnSave.Text = GetLangString("Save");
        }
        private void BindProvince()
        {
            DataSet pds = DbHelperSQL.Query("select  * from province");
            this.ddlProvince.DataSource = pds;
            this.ddlProvince.DataValueField = "ProvinceID";
            this.ddlProvince.DataTextField = "ProvinceName";
            this.ddlProvince.DataBind();
            this.ddltxtDealerLive.SelectedValue = "1";
        }

    }
}